package com.ruoyi.bookkeeping.service.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.bookkeeping.domain.BookAsset;
import com.ruoyi.bookkeeping.domain.BookFriends;
import com.ruoyi.bookkeeping.domain.param.AddBookNoticeParam;
import com.ruoyi.bookkeeping.domain.param.UpdateBookNoticeParam;
import com.ruoyi.bookkeeping.domain.vo.BookBeNoticeVo;
import com.ruoyi.bookkeeping.mapper.BookAssetMapper;
import com.ruoyi.bookkeeping.mapper.BookFriendsMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import com.ruoyi.bookkeeping.mapper.BookNoticeMapper;
import com.ruoyi.bookkeeping.domain.BookNotice;
import com.ruoyi.bookkeeping.service.IBookNoticeService;

/**
 * 好友请求Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-02-24
 */
@Service
public class BookNoticeServiceImpl  extends ServiceImpl<BookNoticeMapper, BookNotice> implements IBookNoticeService
{
    @Autowired
    private BookNoticeMapper bookNoticeMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private BookFriendsMapper bookFriendsMapper;

    /**
     * 查询好友请求
     * 
     * @param friendsNoticeId 好友请求主键
     * @return 好友请求
     */
    @Override
    public BookNotice selectBookNoticeByFriendsNoticeId(Long friendsNoticeId)
    {
        return bookNoticeMapper.selectBookNoticeByFriendsNoticeId(friendsNoticeId);
    }

    /**
     * 查询被好友请求列表
     * @param userId
     * @return
     */
    @Override
    public List<BookBeNoticeVo> selectBookNoticeList(Long userId)
    {
        LambdaQueryWrapper<BookNotice> bookNoticeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bookNoticeLambdaQueryWrapper.eq(BookNotice::getBeUserId,userId);
        List<BookNotice> bookNotices = baseMapper.selectList(bookNoticeLambdaQueryWrapper);
        List<BookBeNoticeVo> bookBeNoticeVos = new ArrayList<>();
        for(BookNotice bookNotice:bookNotices){
            BookBeNoticeVo bookBeNoticeVo = new BookBeNoticeVo();
            BeanUtils.copyProperties(bookNotice,bookBeNoticeVo);
            bookBeNoticeVos.add(bookBeNoticeVo);
        }
        //根据时间排序
        bookNotices.sort((u1,u2) -> u2.getCreateTime().compareTo(u1.getCreateTime()));
        return bookBeNoticeVos;
    }

    /**
     * 新增好友请求
     * 
     * @param bookNotice 好友请求
     * @return 结果
     */
    @Override
    public int insertBookNotice(AddBookNoticeParam addBookNoticeParam, Long userId)
    {
        if(ObjectUtil.equal(userId,addBookNoticeParam.getBeUserId())){
            return 2;
        }
        LambdaQueryWrapper<BookFriends> bookFriendsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bookFriendsLambdaQueryWrapper.eq(BookFriends::getUserId,userId)
                .eq(BookFriends::getFriendsId,addBookNoticeParam.getBeUserId());
        BookFriends bookFriends = bookFriendsMapper.selectOne(bookFriendsLambdaQueryWrapper);
        if(!ObjectUtil.isEmpty(bookFriends)){
            return 0;
        }
        BookNotice bookNotice = new BookNotice();
        BeanUtils.copyProperties(addBookNoticeParam,bookNotice);
        bookNotice.setCreateTime(DateUtils.getNowDate());
        bookNotice.setUserId(userId);
        bookNotice.setCreateBy(sysUserMapper.selectUserById(userId).getNickName());
        return baseMapper.insert(bookNotice);
    }

    /**
     * 修改好友请求
     * @param updateBookNoticeParam
     * @param userId
     * @return
     */
    @Override
    public int updateBookNotice(UpdateBookNoticeParam updateBookNoticeParam,Long userId)
    {
        BookNotice bookNotice = new BookNotice();
        BeanUtils.copyProperties(updateBookNoticeParam,bookNotice);
        LambdaQueryWrapper<BookNotice> bookNoticeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bookNoticeLambdaQueryWrapper.eq(BookNotice::getBeUserId,userId)
                .eq(BookNotice::getFriendsNoticeId,updateBookNoticeParam.getFriendsNoticeId());
        BookNotice bookNotice1 = baseMapper.selectOne(bookNoticeLambdaQueryWrapper);
        if(!ObjectUtil.equal(userId,bookNotice1.getBeUserId())){
            return 0;
        }
        bookNotice.setUpdateTime(DateUtils.getNowDate());
        bookNotice.setUpdateBy(sysUserMapper.selectUserById(userId).getNickName());
        if("0".equals(updateBookNoticeParam.getIsAgree())){
            BookFriends bookFriends = new BookFriends();
            bookFriends.setUserId(userId);
            bookFriends.setFriendsId(bookNotice1.getUserId());
            bookFriends.setFriendsRemark(sysUserMapper.selectUserById(bookNotice1.getUserId()).getNickName());
            bookFriendsMapper.insert(bookFriends);
            BookFriends bookFriends1 = new BookFriends();
            bookFriends1.setUserId(bookNotice1.getUserId());
            bookFriends1.setFriendsId(userId);
            bookFriends1.setFriendsRemark(sysUserMapper.selectUserById(userId).getNickName());
            bookFriendsMapper.insert(bookFriends1);
        }
        return baseMapper.updateById(bookNotice);
    }

}
